<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\SoftDeletes;

class Role extends Model
{
    use SoftDeletes;

    /**
     * 需要转换成日期的属性
     * @var array
     */
    protected $dates = ['deleted_at'];

    /**
     * 获取角色列表
     * @return array
     * @author: lxy
     * @time: 2020/6/5 10:43 上午
     * @流程:
     */
    static public function GetList() : array {
        $list = Role::all()
            ->toArray();

        return [
            'ok'      => true,
            'list'    => $list
        ];
    }

    /**
     * 更新角色状态 启用｜禁用
     * @param array $data
     * @return array
     * @author: lxy
     * @time: 2020/6/5 10:48 上午
     * @流程:
     */
    static public function UpdateStatus(array $data) : array {
        try {
            if (!is_numeric($data['id'])) {
                throw new \Exception('请选择操作信息');
            }

            if (!is_numeric($data['status'])) {
                throw new \Exception('请选择更新状态');
            }
            $res = Role::where('id',$data['id'])->update(['status' => $data['status']]);
            if (!is_numeric($res)) {
                throw new \Exception('数据库更新失败');
            }

            $result = [
                'ok'  => true,
                'msg' => '操作完成'
            ];

            Cache::forget('UserRoleCache');     //所有工作人员权限菜单缓存

        } catch (\Exception $e){
            $result = [
                'ok'  => false,
                'msg' => $e->getMessage()
            ];
        }

        return $result;
    }

    /**
     * 更新工作人员状态 启用｜禁用
     * @param array $data
     * @return array
     * @author: lxy
     * @time: 2020/6/12 7:19 下午
     * @流程:
     */
    static public function UpdateManageStatus(array $data) : array {
        try {
            if (!is_numeric($data['id'])) {
                throw new \Exception('请选择操作信息');
            }

            if (empty($data['status'])) {
                throw new \Exception('请选择更新状态');
            }

            $res = User::where('id',$data['id'])
                ->where('is_staff',1)
                ->update(['status' => $data['status']]);
            if (!is_numeric($res)) {
                throw new \Exception('数据库更新失败');
            }

            $result = [
                'ok'  => true,
                'msg' => '操作完成'
            ];

            Cache::forget('UserRoleCache');     //所有工作人员权限菜单缓存

        } catch (\Exception $e){
            $result = [
                'ok'  => false,
                'msg' => $e->getMessage()
            ];
        }

        return $result;
    }
    /**
     * 删除角色
     * @param $id
     * @return array
     * @author: lxy
     * @time: 2020/6/5 11:13 上午
     * @流程:
     */
    static public function DelRole($id) : array {
        $res = Role::where('id',$id)->delete();
        if ($res) {
            Cache::forget('UserRoleCache');     //所有工作人员权限菜单缓存
            $result = [
                'ok'  => true,
                'msg' => '操作完成'
            ];
        } else {
            $result = [
                'ok'  => false,
                'msg' => '数据库更新失败'
            ];
        }
        return $result;
    }


    /**
     * @param $id
     * @return array
     * @author: lxy
     * @time: 2020/6/12 7:29 下午
     * @流程:删除工作人员
     */
    static public function DelRoleManage($id): array
    {
        $res = User::where('id',$id)
            ->where('is_staff',1)
            ->delete();
        if ($res) {
            $result = [
                'ok'  => true,
                'msg' => '操作完成'
            ];
        } else {
            $result = [
                'ok'  => false,
                'msg' => '数据库更新失败'
            ];
        }
        return $result;
    }

}